package com.finace.management.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.finace.mapper.TbSchoolMapper;
import com.finace.pojo.TbSchool;
import com.finace.pojo.TbSchoolExample;
import com.finace.pojo.TbSchoolExample.Criteria;
import com.finace.management.service.SchoolService;

import com.entity.PageResult;

/**
 * 服务实现层
 * @author Administrator
 *
 */
@Service
public class SchoolServiceImpl implements SchoolService {

	@Autowired
	private TbSchoolMapper schoolMapper;
	
	/**
	 * 查询全部
	 */
	@Override
	public List<TbSchool> findAll() {
		return schoolMapper.selectByExample(null);
	}

	/**
	 * 按分页查询
	 */
	@Override
	public PageResult findPage(int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);		
		Page<TbSchool> page=   (Page<TbSchool>) schoolMapper.selectByExample(null);
		return new PageResult(page.getTotal(), page.getResult());
	}

	/**
	 * 增加
	 */
	@Override
	public void add(TbSchool school) {
		schoolMapper.insert(school);		
	}

	
	/**
	 * 修改
	 */
	@Override
	public void update(TbSchool school){
		schoolMapper.updateByPrimaryKey(school);
	}	
	
	/**
	 * 根据ID获取实体
	 * @param id
	 * @return
	 */
	@Override
	public TbSchool findOne(int id){
		return schoolMapper.selectByPrimaryKey(id);
	}

	/**
	 * 批量删除
	 */
	@Override
	public void delete(int[] ids) {
		for(int id:ids){
			schoolMapper.deleteByPrimaryKey(id);
		}		
	}
	
	
		@Override
	public PageResult findPage(TbSchool school, int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		
		TbSchoolExample example=new TbSchoolExample();
		Criteria criteria = example.createCriteria();
		
		if(school!=null){			
						if(school.getPic()!=null && school.getPic().length()>0){
				criteria.andPicLike("%"+school.getPic()+"%");
			}
			if(school.getName()!=null && school.getName().length()>0){
				criteria.andNameLike("%"+school.getName()+"%");
			}
			if(school.getIpaddress()!=null && school.getIpaddress().length()>0){
				criteria.andIpaddressLike("%"+school.getIpaddress()+"%");
			}
			if(school.getBanner()!=null && school.getBanner().length()>0){
				criteria.andBannerLike("%"+school.getBanner()+"%");
			}
			if(school.getVersion()!=null && school.getVersion().length()>0){
				criteria.andVersionLike("%"+school.getVersion()+"%");
			}
	
		}
		
		Page<TbSchool> page= (Page<TbSchool>)schoolMapper.selectByExample(example);		
		return new PageResult(page.getTotal(), page.getResult());
	}
	
}
